const app = require("express")();
const port = 9025;

// const io = require("socket.io").listen(server);

// let body = "type in text";
// io.sockets.on("connection", function (socket) {
//   socket.emit("refresh", { body: body });

//   socket.on("refresh", function (body_) {
//     console.log("new body");
//     body = body_;
//     console.log(body);
//     console.log("-----");
//     // save(body);
//   });

//   // console.log("user connected!");
//   socket.on("change", function (op) {
//     console.log(op);
//     console.log("change hello");
//     if (
//       op.origin == "+input" ||
//       op.origin == "paste" ||
//       op.origin == "+delete"
//     ) {
//       socket.broadcast.emit("change", op);
//     }
//   });
// });
var wc = require('sensitive-word-filter')
const cors = require("cors");
const bodyParser = require("body-parser");
const multipart = require("connect-multiparty");
const { router, routeConfigs } = require("./router")
const messageCtrl = require("./controller/messageCtrl");
const tool = require("./tool");

app.use(multipart({ uploadDir: "./tempFile" }));

let mode = "dev"
const guard = (req, res, next) => {
  let timer = req.query.tToken
  let now = new Date().getTime() - 1000000000000
  if (mode != "dev" && (!timer || now - Number(tool.decodeTimer(timer)) > 10000)) {
    res.send({ code: -3 })
    return
  }
  next()
}
app.use(guard)


app.use(cors());
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))


let nRouter = new router(app)
nRouter.addRoute(routeConfigs)

app.get("/qqface/:faceid", messageCtrl.face);

app.get("/psd/:psdid", messageCtrl.psd);

app.get("/", (req, res) => {
  res.send("haust 软件学院 软件传媒综合api平台");
});

app.listen(port, () => {
  console.log(`Example app listening on http://127.0.0.1:${port}`);
});

process.on('uncaughtException', function (err) {
  console.log(err)
})



// DAV 80G
// Gitee 500MB
// lanzou
// MS 1024G